package com.gc.repository.impl;

import com.gc.entity.BookSeller;
import com.gc.entity.Manager;
import com.gc.repository.ManagerRepository;
import com.gc.util.C3p0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 *
 *      Long id;
 *     String name;
 *     String aid;
 *     String password;
 *     String phone;
 *     String email;
 */
public class ManagerRepositoryImpl implements ManagerRepository {
    @Override
    public boolean Add(Manager manager) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            qr.update("insert into manager(name, aid,  password, phone, email) VALUES (?, ?, ?, ?, ?)",
                    manager.getName(),
                    manager.getAid(),
                    manager.getPassword(),
                    manager.getPhone(),
                    manager.getEmail()
            );
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Manager> FindAll() {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            List<Manager> query = qr.query("select * from manager", new BeanListHandler<>(Manager.class));

            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean DelById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("delete from manager WHERE id = ?", id);
            System.out.println(res);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Manager FindById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            Manager res = qr.query("select * from manager WHERE id = ?", new BeanHandler<>(Manager.class),
                    id);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Manager FindByAdminId(String aid) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            Manager res = qr.query("select * from manager WHERE aid = ?", new BeanHandler<>(Manager.class),
                    aid);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean UpdateById(Manager manager) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("update manager set name = ?, aid = ?, password = ?, phone = ?, email = ? WHERE id = ?",
                    manager.getName(),
                    manager.getAid(),
                    manager.getPassword(),
                    manager.getPhone(),
                    manager.getEmail(),
                    manager.getId());
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
